/**
 * 初始化首页详情对话框
 */
var ApphomeInfoDlg = {
    apphomeInfoData : {}
};

/**
 * 清除数据
 */
ApphomeInfoDlg.clearData = function() {
    this.apphomeInfoData = {};
}
/**
 * 验证数据是否为空
 */
ApphomeInfoDlg.validate = function () {
    $('#indexFrom').bootstrapValidator('validate');
    return $("#indexFrom").data('bootstrapValidator').isValid();
}
/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ApphomeInfoDlg.set = function(key, val) {
    if(key == 'zhuanji') {
        if($("#" + key).attr("zhuanjis") == null || $("#" + key).attr("zhuanjis") == "undefined") {
            var checkedCatesId = "";
            $("input:checkbox[name='cate_name']:checked").each(function(i) {
                if(i == 0) {
                    checkedCatesId = $(this).val();
                } else {
                    checkedCatesId += "," + $(this).val();
                }

            });
            $("#" + key).attr("zhuanjis",checkedCatesId);
        }
        this.apphomeInfoData[key] = (typeof value == "undefined") ? $("#" + key).attr("zhuanjis") : value;
    }else{
        this.apphomeInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    }

    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ApphomeInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ApphomeInfoDlg.close = function() {
    parent.layer.close(window.parent.Apphome.layerIndex);
}
/**
 * 获得专辑并放入input中
 */
//显示的名称
var titlefenlei="";
var fenleiName=""
ApphomeInfoDlg.getCates = function () {
    var checkedCatesId = "";
    var checkedCatesName = "";
    $("input:checkbox[name='cate_name']:checked").each(function(i) {
        if(i == 0) {
            checkedCatesId = $(this).val();
            checkedCatesName = $(this).attr("cate-name");
        } else {
            checkedCatesId += "," + $(this).val();
            checkedCatesName += "," + $(this).attr("cate-name");
        }

    });
    if (checkedCatesName!=""){
        console.log("我是专辑")
        titlefenlei=checkedCatesName;
    }else{
        console.log("我是分类")
        titlefenlei=fenleiName;
    }

    $("#zhuanji").attr("zhuanjis",checkedCatesId);
    $("#zhuanji").val(titlefenlei);
    // $("#zhuanji").attr("zhuanjis",checkedCatesId);
    // $("#zhuanji").val(checkedCatesName);
    $("#cateModal").modal('hide');
};
/**
 * 获得专辑并放入input中以便修改
 */

ApphomeInfoDlg.getCatesUp = function () {
    var checkedCatesId = "";
    var checkedCatesName = "";
    $("input:checkbox[name='cate_name']:checked").each(function(i) {
        if(i == 0) {
            checkedCatesId = $(this).val();
            checkedCatesName = $(this).attr("cate-name");
        } else {
            checkedCatesId += "," + $(this).val();
            checkedCatesName += "," + $(this).attr("cate-name");
        }

    });
    if (checkedCatesName!=""){
        console.log("我是专辑")
        titlefenlei=checkedCatesName;
    }else{
        console.log("我是分类")
        titlefenlei=fenleiName;
    }

    $("#zhuanji").attr("zhuanjis",checkedCatesId);
    $("#zhuanji").val(titlefenlei);
    $("#cateModal").modal('hide');
};
/**
 * 通过栏目查询下面的专辑
 */
var checkedIds = [];
$("#banners").change(function () {
    // alert("成功")
    //清空选中的专辑
    var checkedBannersId = "";
    var checkedBannersName = "";
    $("input:radio[name='banner_name']:checked").each(function(i) {
        console.log("进来了")
        if(i == 0) {
            checkedBannersId = $(this).val();
            checkedBannersName = $(this).attr("banner_name");
        } else {
            checkedBannersId += "," + $(this).val();
            checkedBannersName += "," + $(this).attr("banner_name");
        }

    });
    fenleiName="";
    fenleiName=checkedBannersName;
    // console.log(checkedBannersId)
    // console.log(checkedBannersName)
    //var selectedBannerId = $(this).children('option:selected').val();
    // $("#cates input[name='cate_name']:checked").each(function () {
    //     if(!containId(checkedIds,$(this).val())) {
    //         checkedIds.push($(this).val());
    //     }
    // });
    console.log(checkedBannersId)
    $("#type").val(checkedBannersId);
    console.log( $("#type").val());
    //音频栏目
    $.get(Feng.ctxPath + "/vedio/radio/cateInBanners?bannerIds=" + 0,function (data) {
        $("#cates").empty();
        for (var i = 0; i< data.length;i++) {
            if(containId(checkedIds,data[i].id)) {
                $("#cates").append("<input class='categroys' name='cate_name' value='"+data[i].id+"' cate-name='"+data[i].name+"' type='checkbox' checked='checked'/>"+data[i].name+"&nbsp;&nbsp;&nbsp;&nbsp;")
            } else {
                $("#cates").append("<input class='categroys' name='cate_name' value='"+data[i].id+"' cate-name='"+data[i].name+"' type='checkbox'/>"+data[i].name+"&nbsp;&nbsp;&nbsp;&nbsp;")
            }

        }
    });
});
$("#cates").change(function () {
    //清空选中的分类
    var checkedBannersId = "";
    var checkedBannersName = "";
    fenleiName="";
    $("input:radio[name='banner_name']:checked").each(function(i) {
        if(i == 0) {
            checkedBannersId = $(this).val();
            checkedBannersName = $(this).attr("banner_name");
        } else {
            checkedBannersId += "," + $(this).val();
            checkedBannersName += "," + $(this).attr("banner_name");
        }

    });
    //清空
    $("#type").val("");
    //音频栏目
    $.get(Feng.ctxPath + "/vedio/radio/cateInBanners2?bannerIds=" + 0,function (data) {
        $("#banners").empty();
        for (var i = 0; i< data.length;i++) {

            //<input class="categroys" name="banner_name" value="${banner.id}" banner_name="${banner.name}" type="radio"/>${banner.name}&nbsp;&nbsp;&nbsp;&nbsp;
            $("#banners").append("<input class='categroys' name='banner_name' value='"+data[i].id+"' banner_name='"+data[i].name+"' type='radio' />"+data[i].name+"&nbsp;&nbsp;&nbsp;&nbsp;")


        }
    });
});
/**
 * 判断是否存在id
 * @param checkIds
 * @param id
 * @returns {boolean}
 */
function containId(checkIds,id) {
    for (var i = 0;i < checkIds.length;i++) {
        if(id == checkIds[i]) {
            return true;
        }
    }
    return false;
}
/**
 * 收集数据
 */
ApphomeInfoDlg.collectData = function() {
    this.set('id').set('classtypeId').set('pageclassId').set('viewTitle').set('type').set('zhuanji').set('img').set('desc').set('sort');
}

/**
 * 提交添加
 */
ApphomeInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/apphome/add", function(data){
        Feng.success("添加成功!");
        window.parent.Apphome.table.refresh();
        ApphomeInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.apphomeInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ApphomeInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/apphome/update", function(data){
        Feng.success("修改成功!");
        window.parent.Apphome.table.refresh();
        ApphomeInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.apphomeInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("indexFrom", ApphomeInfoDlg.validateFields);
    // 初始化头像上传
    var iconUp = new $WebIconUpload("img");
    iconUp.setUploadBarId("progressBar");
    iconUp.init();


    //初始化是否是菜单
    if($("#classtypeIdValue").val() == undefined){
        $("#classtypeId").val(0);
    }else{
        $("#classtypeId").val($("#classtypeIdValue").val());
    }

    if($("#pageclassIdValue").val() == undefined){
        $("#pageclassId").val(0);
    }else{
        $("#pageclassId").val($("#pageclassIdValue").val());
    }
});
